const {
    EditorElement
} = require('./editor-element');

class Point extends EditorElement {

    static className() {
        return 'Point';
    }

    constructor(x, y) {
        super();
        this._x = x;
        this._y = y;
    }

    static zero() {
        return new this(0, 0);
    }

    static isPoint(point) {
        try {
            return point && point.x !== null && point.x !== undefined && point.y !== null && point.y !== undefined;
        } catch (e) {
            console.warn(e);
            return false;
        }
    }

    static pointOffset(point1, point2) {
        if (this.isPoint(point1) && this.isPoint(point2)) {
            return point1.offsetWithPoint(point2);
        }
        return null;
    }

    offsetWithPoint(point) {
        if (Point.isPoint(point)) {
            return new Point(point.x - this.x, point.y - this.y);
        }
        return null;
    }

    static distance(point1, point2) {
        if (this.isPoint(point1) && this.isPoint(point2)) {
            return this.distanceWithXY(point1.x, point1.y, point2.x, point2.y);
        }
        return 0;
    }

    static distanceWithXY(x1, y1, x2, y2) {
        return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
    }

    setupWithPoint(point) {
        if (this.constructor.isPoint(point)) {
            this.x = point.x;
            this.y = point.y;
        } else {
            console.log(`setupWithPoint: ${point} 无效的Point对象`);
        }
    }

    move(size) {
        if (size && size.constructor.isSize(size)) {
            this.x += size.width;
            this.y += size.height;
        }
    }

    get x() {
        return this._x;
    }

    set x(value) {
        this._x = value;
    }

    get y() {
        return this._y;
    }

    set y(value) {
        this._y = value;
    }

    scale(value) {
        this.x *= value;
        this.y *= value;
    }

    clone() {
        if (Point.isPoint(this)) {
            return new Point(this.x, this.y);
        }
        return null;
    }
}

module.exports = {
    Point
};